//
//  FPBankCardInfoMainView.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/1/23.
//

#import "FPBankCardInfoMainView.h"
#import "FPBankCardModel.h"
#import "FPBankCardInfoCell.h"

@interface FPBankCardInfoMainView () <UITableViewDelegate , UITableViewDataSource>

@property (nonatomic , strong) UITableView *tableView;

@end

@implementation FPBankCardInfoMainView

- (void)setDataSource:(NSArray *)dataSource {
    
    _dataSource = dataSource;
    
    CGFloat height;
    if (dataSource.count == 0) {
        
        self.lineView.hidden = YES;
        height = 68.f * dataSource.count;
    }
    else if (dataSource.count < 6) {
        
        height = 68.f * dataSource.count;
    }
    else {
        
        height = 68.f * 6;
    }
    
    self.tableView.frame = CGRectMake(0.f, 0.f, SCREEN_WIDTH, height);
    
    [self.tableView reloadData];
}

- (void)awakeFromNib {
    
    [super awakeFromNib];
    
    self.tipLab.textColor = [UIColor hexColor:@"#3A3A3A"];
    self.tipLab2.textColor = [UIColor hexColor:@"#3A3A3A"];
    
    self.backgroundColor = UIColor.whiteColor;
    
    self.lineView.backgroundColor = [UIColor hexColor:@"#F4F6FA"];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.backView.bounds style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.backView addSubview:self.tableView];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.rowHeight = 68.f;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FPBankCardInfoCell *cell;
    
    if (cell == nil) {
        
        cell = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(FPBankCardInfoCell.class) owner:nil options:nil].lastObject;
    }
    
    FPBankCardModel *model = self.dataSource[indexPath.row];
    
    cell.mainLab.text = [NSString stringWithFormat:@"%@(%@)", model.bankName , [model.bankCard substringFromIndex:(model.bankCard.length - 4)]];
//    cell.mainImgView
//    cell.mainLab.text = [NSString stringWithFormat:@"%@(%@)", model.bankName , [model.cardNumber substringFromIndex:(model.cardNumber.length - 4)]];
//    cell.mainImgView.image = IMG(model.logo.length > 0 ? model.logo : @"icon_bank_card_default");
//    [cell.mainImgView sd_setImageWithURL:[NSURL URLWithString:[MAIN_IMAGE_URL stringByAppendingString:model.imagePath]] placeholderImage:IMG(@"icon_bank_card_default")];
    [cell.mainImgView sd_setImageWithURL:[NSURL URLWithString:[FPUserModel.globalHeadURL stringByAppendingString:model.imagePath]] placeholderImage:IMG(@"icon_bank_card_default")];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    self.selectBankCardBlock(indexPath.row);
}


@end
